﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

namespace EducationManagementSystem.Zr
{
    public partial class stuInfodetails : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Bind();
            }
        }

        /// <summary>
        /// 班级的绑定和判断此时是什么请求
        /// </summary>
        private void Bind()
        {

            SqlConnection conn = DB.Conn();
            conn.Open();
            string sql = "select b.className,b.classID from Zhuren a,class b where zhurenID='" + Session["zhurenid"].ToString() + "' and a.schoolID=b.schoolID";
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataReader ds = cmd.ExecuteReader();   //读取形式，也可以是用Dataset的形式
            DropDownList2.DataSource = ds;
            DropDownList2.DataTextField = "className";   //绑定班级
            DropDownList2.DataValueField = "classID"; //绑定班级编号
            DropDownList2.DataBind();


            if (Request.QueryString["Flag"] == "Add")
            {
                //清空控件
                this.txtID.Text = "";
                this.txtXm.Text = "";

            }
            else
            {
                //根据修改按钮传过来的学生id查找
                txtID.Text = Request.QueryString["ID"];
                txtID.Enabled = false;
                SqlConnection conn1 = DB.Conn();
                conn1.Open();
                string sql1 = "select b.className,stuName,sex,age,mz,syd,birthTime,rxrq,b.classid from  Student a,Class b   where stuid='" + Request.QueryString["ID"] + "' and a.classID=b .classID";
                SqlCommand cmd1 = new SqlCommand(sql1, conn1);
                //cmd1.ExecuteNonQuery();
                DataSet ds1 = new DataSet();
                SqlDataAdapter sda = new SqlDataAdapter(cmd1);
                sda.Fill(ds1);
                DropDownList2.SelectedValue = ds1.Tables[0].Rows[0][8].ToString();//绑
                txtXm.Text = ds1.Tables[0].Rows[0][1].ToString();//姓名
                if (ds1.Tables[0].Rows[0][2].ToString() == "男")
                {
                    this.DropDownList1.SelectedIndex = 0;
                }
                else
                {
                    this.DropDownList1.SelectedIndex = 1;
                }
                TextBox1.Text = ds1.Tables[0].Rows[0][3].ToString();//年龄
                TextBox2.Text = ds1.Tables[0].Rows[0][4].ToString();//民族
                TextBox3.Text = ds1.Tables[0].Rows[0][5].ToString();//生源地
                TextBox4.Text = ds1.Tables[0].Rows[0][6].ToString();//出生日期
                TextBox5.Text = ds1.Tables[0].Rows[0][7].ToString();//入学日期
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            //返回
            Response.Redirect("StuInfoMange.aspx");
        }

        //添加之后的动作
        protected void Button1_Click(object sender, EventArgs e)
        {
            SqlConnection conn = DB.Conn();
            conn.Open();
            string sql = "select * from Student where stuid='" + this.txtID.Text + "'";
            SqlCommand cmd = new SqlCommand(sql, conn);
            object a = cmd.ExecuteScalar();
            if (Request.QueryString["Flag"] == "Add")
            {

                //判断信息是否正确
                if (this.txtID.Text == "")
                {
                    this.Terr.InnerHtml = "<font color='red'>学号不能为空</font>";
                    return;
                }
                else
                {
                    if (a != null)
                    {
                        this.Terr.InnerHtml = "<font color='red'>学号重复</font>";
                        return;
                    }
                    else
                    {
                        string sql2 = "insert into student(stuid,stuname,sex,age,classid,mz,syd,birthtime,rxrq) values('" + txtID.Text + "','" + txtXm.Text + "','" + this.DropDownList1.SelectedItem.Text + "','" + this.TextBox1.Text + "'," + this.DropDownList2.SelectedValue + ",'" + this.TextBox2.Text + "','" + this.TextBox3.Text + "','" + this.TextBox4.Text + "','" + this.TextBox5.Text + "')";
                        SqlCommand cmd2 = new SqlCommand(sql2, conn);
                        int c = cmd2.ExecuteNonQuery();

                        if (c > 0)
                        {
                            this.Page.RegisterStartupScript(" ", "<script>alert('添加成功'); </script> ");

                        }
                        else
                        {

                            this.Page.RegisterStartupScript(" ", "<script>alert('添加失败 '); </script> ");
                        }
                    }
                }
            }
            else
            {
                //修改学生信息
                string sql3 = "update student set  mz='" + this.TextBox2.Text + "',syd='" + this.TextBox3.Text + "',birthtime='" + this.TextBox4.Text + "',rxrq='" + this.TextBox5.Text + "', stuname='" + this.txtXm.Text + "',sex='" + this.DropDownList1.SelectedItem.Text + "',age='" + this.TextBox1.Text + "',classid=" + this.DropDownList2.SelectedValue + " where stuid='" + this.txtID.Text + "'";
                SqlCommand cmd3 = new SqlCommand(sql3, conn);
                int c = cmd3.ExecuteNonQuery();
                if (c > 0)
                {
                    this.Page.RegisterStartupScript(" ", "<script>alert('修改成功'); </script> ");
                    Response.Redirect("StuInfoMange.aspx");
                }
                else
                {

                    this.Page.RegisterStartupScript(" ", "<script>alert('修改失败 '); </script> ");
                }
            }

        }
    }
}